@extends('layouts.mainlayout')

@section('main')
<section class="content">     
    <div class="panel panel-default">
        <div class="panel-heading">Thông Tin Cá Nhân</div>
        <div class='panel-body'>
            <form class="form-horizontal" action="<?php echo asset('user/infouser'); ?>" method="POST" role="form" name="edit_info" id="edit_info">
                <fieldset>
                    <div class="form-group">
                        <div class="col-lg-12 avatar" style="text-align:center">

                            <img src="<?php echo Auth::user()->anh_dai_dien; ?>" alt="Ảnh đại diện" title="Ảnh đại diện" width="128px" height="128px" style="padding:3px; border: 1px solid #DDD; box-shadow: 2px 2px 2px #DDD;" class="anh_dai_dien" id="anh_dai_dien">
                            <div class="btn btn-primary edit-avatar" onclick="BrowseServer();" title="Chọn ảnh đại diện" style="position: absolute;z-index: 5;display: none;bottom: 47px;left: 50%;margin-left: -18px;"><i class="fa fa-edit"></i></div>
                        </div>
                    </div>

                    <div class="form-group">
                        {{ Form::label('username', 'Tài Khoản:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-9">
                            {{ Form::text('username',$tai_khoan->username,array('class'=>'form-control ','disabled')) }}
                        </div>
                    </div>
                    <div class="form-group">
                        {{ Form::label('mascb', 'Mã Cán Bộ:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-9">
                            {{ Form::text('mascb',$tai_khoan->mascb,array('class'=>'form-control ')) }}
                        </div>
                    </div>
                    <div class="form-group">
                        {{ Form::label('ho_cb', 'Họ:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-3">
                            {{ Form::text('ho_cb',$tai_khoan->ho_cb,array('class'=>'form-control ')) }}
                        </div>
                        {{ Form::label('ten_cb', 'Tên:',array('class'=>'col-lg-1 control-label')) }}
                        <div class="col-lg-5">
                            {{ Form::text('ten_cb',$tai_khoan->ten_cb,array('class'=>'form-control ')) }}
                        </div>
                    </div>

                    <div class="form-group">
                        {{ Form::label('ngay_sinh_cb', 'Ngày Sinh:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-9">
                            {{ Form::text('ngay_sinh_cb',$tai_khoan->ngay_sinh_cb,array('class'=>'form-control ')) }}
                        </div>
                    </div>

                    <div class="form-group">
                        {{ Form::label('gioi_tinh_cb', 'Giới Tính:',array('class'=>'col-lg-3 control-label'))}}
                        <div class="col-lg-9"> 
                            <select name="gioi_tinh_cb" id="gioi_tinh_cb" class="form-control" required="required">
                                <option value="" <?php if($tai_khoan->gioi_tinh_cb=="") echo 'selected';?>>Chọn</option>
                                <option value="Nam" <?php if($tai_khoan->gioi_tinh_cb=="Nam") echo 'selected';?>>Nam</option>
                                <option value="Nữ" <?php if($tai_khoan->gioi_tinh_cb=="Nữ") echo 'selected';?>>Nữ</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        {{ Form::label('email_cb', 'Email:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-9">
                            {{ Form::text('email_cb',$tai_khoan->email_cb,array('class'=>'form-control ')) }}
                        </div>
                    </div>

                    <div class="form-group">
                        {{ Form::label('sdt_cb', 'Số Điện Thoại:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-9">
                            {{ Form::text('sdt_cb',$tai_khoan->sdt_cb,array('class'=>'form-control ')) }}
                        </div>
                    </div>

                    <div class="form-group">
                        {{ Form::label('dia_chi_cb', 'Địa Chỉ:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-9">
                            {{ Form::text('dia_chi_cb',$tai_khoan->dia_chi_cb,array('class'=>'form-control ')) }}
                        </div>
                    </div>

                    <div class="form-group">
                        {{ Form::label('chuc_danh', 'Chức Danh:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-9">
                            {{ Form::text('chuc_danh',$tai_khoan->chuc_danh,array('class'=>'form-control ')) }}
                        </div>
                    </div>

                    <div class="form-group">
                        {{ Form::label('chuc_vu', 'Chức Vụ:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-9">
                            {{ Form::text('chuc_vu',$tai_khoan->chuc_vu,array('class'=>'form-control ')) }}
                        </div>
                    </div>
                    <div class="form-group">
                        
                        <div class="col-lg-12" style="text-align:center">
                            <button type="submit" class="btn btn-primary">Cập Nhật</button>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-lg-12" id="noti"></div>
                    </div>
                </fieldset>
            </form>
        </div>
    </div>
</section><!-- /.content -->
<script src="<?php echo asset('public/plugins/ckfinder/ckfinder.js'); ?>"></script>
<script src="<?php echo asset('public/plugins/ckeditor/adapters/jquery.js'); ?>"></script>
<script type="text/javascript">
    $('#edit_info').submit(function(event) {
        $("#edit_info input").prop("disabled", true);
        $("#edit_info select").prop("disabled", true);
        $('#noti').html('<br/><div class="alert alert-warning fade in"><p>Đang cập nhật tài khoản</p></div>');

            var ho_cb= $('#ho_cb').val();
            var ten_cb = $('#ten_cb').val();
            var email_cb= $('#email_cb').val();
            var ngay_sinh_cb= $('#ngay_sinh_cb').val();
            var sdt_cb= $('#sdt_cb').val();
            var gioi_tinh_cb = $('#gioi_tinh_cb').val();
            var dia_chi_cb = $('#dia_chi_cb').val();
            var chuc_danh = $('#chuc_danh').val();
            var chuc_vu = $('#chuc_vu').val();
            $.ajax({
                url: '<?php echo asset("user/infouser")?>',
                type: 'post',
                dataType: 'json',
                data: {
                    ho_cb: ho_cb,
                    ten_cb: ten_cb,
                    email_cb: email_cb,
                    ngay_sinh_cb: ngay_sinh_cb,
                    sdt_cb: sdt_cb,
                    gioi_tinh_cb: gioi_tinh_cb,
                    dia_chi_cb:dia_chi_cb,
                    chuc_danh:chuc_danh,
                    chuc_vu:chuc_vu
                },
            })
            .done(function(data) {
                if(data == 1){
                    $('#noti').html('<br/><div class="alert alert-info fade in"><p>Cập nhật tài khoản thành công.</p></div>');
                    window.location.replace('<?php echo asset("/")?>');
                }
                // console.log(data);
            })
            .fail(function() {
                $("#edit_info input").prop("disabled", false);
                $("#edit_info select").prop("disabled", false);
                $('#noti').html('<br/><div class="alert alert-warning fade in"><p>Thất bại. Vui lòng kiểm tra lại thông tin hoặc tải lại trang</p></div>');
            })
            .always(function() {
                console.log("complete");
            });

        return false;
    });

    function BrowseServer()
    {
        CKFinder.popup( { basePath : '<?php echo asset("public/plugins/ckfinder/"); ?>', selectActionFunction : SetFileField } ) ;
    }
    function SetFileField( fileUrl )
    {
        $.ajax({
            url: '<?php echo asset("ajax/updateavatar")?>',
            type: 'post',
            dataType: 'html',
            data: {img_new_url :fileUrl },
        })
        .done(function(data) {
            $('.anh_dai_dien').attr("src", data);
            console.log(data);
            console.log("success");
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }

    $('.avatar').hover(function() {
        $('.edit-avatar').show();
    }, function() {
        $('.edit-avatar').hide();
    });
</script>
@stop